//
//  TF_AlbumView.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2021/12/7.
//  Copyright © 2021 Talkfun. All rights reserved.
//

import UIKit

class TF_AlbumView: UIView,UITableViewDelegate,UITableViewDataSource{

    var titleLab:UILabel!
    var closeBtn:UIButton!
    var tableview:UITableView!
    var datasource:[TFPlaybackAlbum] = []
    var selectedArray:[Int] = []
    var playbackID:String!
    @objc public var setAlbumBlock: ((String) -> ())?
    @objc public var isShow:Bool = false  //记录是否显示该view
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.titleLab = UILabel.init()
        self.titleLab.text = "专辑";
        self.titleLab.textColor = UIColor(red: 48/255.0, green: 51/255.0, blue: 75/255.0, alpha: 1.0)
        
        self.titleLab.textAlignment = .center;
        self.titleLab.font = UIFont.PingFang(size: 18)
        addSubview(self.titleLab)
       
        self.closeBtn = UIButton.init()
        self.closeBtn.setImage(UIImage.init(named: "HTLive.bundle/close_gray"), for: .normal)
        self.closeBtn.addTarget(self, action: #selector(closeBtnClick), for: .touchUpInside)
        addSubview(self.closeBtn)
        
        self.tableview = UITableView.init(frame: CGRect.zero)
    
        self.tableview.delegate = self
        self.tableview.dataSource = self
        self.tableview.backgroundColor = UIColor.white
        self.tableview.register(TF_AlbumCell.self, forCellReuseIdentifier: "album")
        self.tableview.rowHeight = 90
        self.tableview.separatorStyle = UITableViewCell.SeparatorStyle.none
        self.addSubview(self.tableview)
       
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.titleLab.frame = CGRect(x: (self.frame.size.width-100)/2, y: 16, width: 100, height: 25)
        self.closeBtn.frame = CGRect(x: self.frame.size.width-10-30, y: 16, width: 30, height: 30)
      
        self.tableview.frame = CGRect(x: 0, y: self.titleLab.frame.maxY+12, width: self.frame.size.width, height: self.frame.size.height-16-25-12)
    }
    
   @objc func closeBtnClick()  {
        
    }
    
    @objc public func updateAlbum(list:NSMutableArray?) {
       
        self.datasource.removeAll()
        list?.forEach({  model1 in
            self.datasource.append(model1 as! TFPlaybackAlbum)
          })
          
          for _ in 0..<self.datasource.count {
              self.selectedArray.append(0)
          }
          
          self.tableview.reloadData()

    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.datasource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "album", for: indexPath) as! TF_AlbumCell
       
    
        
        cell.cellwidth = self.frame.size.width
        let model:TFPlaybackAlbum = self.datasource[indexPath.row]
        cell.selectedArray = self.selectedArray
        cell.setAlbumModel(model: model)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        self.datasource.forEach { album in
            album.select = false
        }
        let Model:TFPlaybackAlbum = self.datasource[indexPath.row];
        Model.select = true
        let duration :String = Model.access_token
           if self.setAlbumBlock != nil {
               self.setAlbumBlock?(duration)
           }
        
        self.tableview.reloadData()
      
    }
    
  
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
